package sortDocs

import (
	"sort"
)

type Pair struct {
	Key   int64
	Value int
}

type PairList []Pair

func (p PairList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p PairList) Len() int           { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value > p[j].Value } //从大到小

func sortMapByValue(m map[int64]int) PairList {
	p := make(PairList, len(m))
	i := 0
	for k, v := range m {
		p[i] = Pair{k, v}
		i++
	}
	sort.Sort(p)
	return p
}

//最简单的一种网页排序
//统计每个网页编号出现的次数，按照出现次数的多少，从大到小排序
func SortDocs(docIDs []int64) (sortedDocIDs []int64) {
	//统计每个网页编号出现的次数
	count := make(map[int64]int)
	for _, docID := range docIDs {
		count[docID]++
	}
	//对次数进行排序
	pairs := sortMapByValue(count)
	//返回已排好顺序的网页编号
	for _, pair := range pairs {
		sortedDocIDs = append(sortedDocIDs, pair.Key)
	}
	return
}
